package org.obm.delivery.cassandra.filters;

import java.util.List;

import org.apache.james.mime4j.dom.Message;
import org.obm.lmtp.backend.LmtpAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RuleEngine {

	private static final Logger logger = LoggerFactory
			.getLogger(RuleEngine.class);

	private String mailbox;
	private LmtpAddress addr;
	private List<IFilterRule> rules;

	private IRuleEngineImplementation engine;

	public RuleEngine(LmtpAddress addr, List<IFilterRule> rules, IRuleEngineImplementation rei) {
		this.mailbox = AddressToMailbox.getMailbox(addr);
		this.addr = addr;
		this.rules = rules;
		this.engine = rei;
		this.engine.setRules(rules);
		logger.info("rule engine initialized with " + this.rules.size()
				+ " rules for mailbox " + mailbox + " ("
				+ this.addr.getEmailAddress() + ")");
	}
	
	public void apply(Message msg) {
		engine.applyTo(addr, msg);
	}

	public DeliveryOrder getOrder() {
		return new DeliveryOrder(DeliveryKind.FOLDER, mailbox + "::INBOX");
	}

}
